package com.yhgarden.bbs.service.impl.business;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yhgarden.bbs.entity.business.WechatUser;
import com.yhgarden.bbs.mapper.business.WechatUserMapper;
import com.yhgarden.bbs.model.enums.StatusCode;
import com.yhgarden.bbs.model.helper.Pager;
import com.yhgarden.bbs.model.helper.SortPointer;
import com.yhgarden.bbs.model.response.BaseResponse;
import com.yhgarden.bbs.service.api.business.WechatUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ExecutorService;

/**
* @author Laijiaxiang
* @since 2019-09-23
*/
@Service
public class WechatUserServiceImpl implements WechatUserService {

    @Autowired
    private WechatUserMapper wechatUserMapper;
    
    @Autowired
    private ExecutorService executorService;
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseResponse createOrUpdate(WechatUser wechatUser) {
        BaseResponse response = new BaseResponse(StatusCode.SUCCESS);
        if(wechatUser.getId() == null){
            String uuid = UUID.randomUUID().toString();
            wechatUser.setId(uuid);
            wechatUserMapper.create(wechatUser);
        }else {
            wechatUserMapper.update(wechatUser);
        }
        response.setData(wechatUser);
        return response;
    }

    @Override
    public BaseResponse getById(String id) {
        BaseResponse response = new BaseResponse(StatusCode.SUCCESS);
        WechatUser wechatUser = wechatUserMapper.getById(id);
        response.setData(wechatUser);
        return response;
    }
    
    @Override
    public BaseResponse getByIdForList(String id) {
        BaseResponse response = new BaseResponse(StatusCode.SUCCESS);
        WechatUser wechatUser = wechatUserMapper.getByIdForList(id);
        response.setData(wechatUser);
        return response;
    }
    
    
    @Override
    public BaseResponse getByIdMoreDetail(String id) {
        BaseResponse response = new BaseResponse(StatusCode.SUCCESS);
        WechatUser wechatUser = wechatUserMapper.getByIdMoreDetail(id);
        response.setData(wechatUser);
        return response;
    }


    @Override
    public BaseResponse query(WechatUser wechatUser) {
        BaseResponse response = new BaseResponse(StatusCode.SUCCESS);
        Pager pager = wechatUser.getPager();
        SortPointer sortPointer = wechatUser.getSortPointer();
        if(sortPointer == null){
            sortPointer = new SortPointer();
        }
        PageHelper.startPage(pager.getCurrentPage(),pager.getPageSize());
        List<WechatUser> wechatUserList = wechatUserMapper.query(wechatUser,sortPointer.getOrder(),sortPointer.getField());
        PageInfo result = new PageInfo<WechatUser>(wechatUserList);
        response.setData(result);
        return response;
    }


    @Override
    public BaseResponse queryForList(WechatUser wechatUser) {
        BaseResponse response = new BaseResponse(StatusCode.SUCCESS);
        Pager pager = wechatUser.getPager();
        SortPointer sortPointer = wechatUser.getSortPointer();
        if(sortPointer == null){
            sortPointer = new SortPointer();
        }
        PageHelper.startPage(pager.getCurrentPage(),pager.getPageSize());
        List<WechatUser> wechatUserList = wechatUserMapper.queryForList(wechatUser,sortPointer.getOrder(),sortPointer.getField());
        PageInfo result = new PageInfo<WechatUser>(wechatUserList);
        response.setData(result);
        return response;
    }


    @Override
    public BaseResponse queryMoreDetail(WechatUser wechatUser) {
        BaseResponse response = new BaseResponse(StatusCode.SUCCESS);
        Pager pager = wechatUser.getPager();
        SortPointer sortPointer = wechatUser.getSortPointer();
        if(sortPointer == null){
            sortPointer = new SortPointer();
        }
        PageHelper.startPage(pager.getCurrentPage(),pager.getPageSize());
        List<WechatUser> wechatUserList = wechatUserMapper.queryMoreDetail(wechatUser,sortPointer.getOrder(),sortPointer.getField());
        PageInfo result = new PageInfo<WechatUser>(wechatUserList);
        response.setData(result);
        return response;
    }


    @Override
    public BaseResponse delete(String id) {
        BaseResponse response = new BaseResponse(StatusCode.SUCCESS);
        wechatUserMapper.delete(id);
        return response;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseResponse batchDelete(List<String> ids) {
        BaseResponse response = new BaseResponse(StatusCode.SUCCESS);
        int deleteCount = wechatUserMapper.batchDelete(ids);
        return response;
    }

}

